package com.clarte.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import bsh.Interpreter;

import com.clarte.common.dao.BaseDao;

public class MyTest {
	
	@Test
	public void test5(){
		double r = 0.035;
		double a1 = 0.2;
		double a2 = 0.6;
		double a3 = 0.8;
		int p = 3650;
		double m = 0;
		double n = 0;
		for(int i=0;i<27;i++){
			if(i<17){
				m = (m+p)*(1+r);
			}else if(i==17||i==18||i==19|i==20){
				n = n + m*a1;
				System.out.print("领取："+m*a1);
				m = (m-m*a1)*(1+r);
				System.out.println("余额："+m);
			}else if(i==22){
				n = n + m*a2;
				System.out.print("领取："+m*a2);
				m = (m-m*a2)*(1+r);
				System.out.println("余额："+m);
			}else if(i==24){
				n = n + m*a3;
				System.out.print("领取："+m*a3);
				m = (m-m*a3)*(1+r);
				System.out.println("余额："+m);
			}else if(i==26){
				n = n + m*a3;
				System.out.print("领取："+m*a3);
				m = (m-m*a3)*(1+r);
				System.out.println("余额："+m);
			}else{
				m=m*(1+r);
			}
		}
		System.out.println("共领取："+n+"余额："+m);
	}
	@Test
	public void test4() throws Exception{
		Interpreter i = new Interpreter();  // Construct an interpreter
		i.set("a98c5c9613809487ba52b54df7a4819bf", 5);                    // Set variables
		i.set("date", new Date() );
		Date date = (Date)i.get("date");    // retrieve a variable
		// Eval a statement and get the result
		i.eval("result = (a98c5c9613809487ba52b54df7a4819bf>10 && a98c5c9613809487ba52b54df7a4819bf<20)");            
		Object o = i.get("result");
		System.out.println(o);
	}
	@Test
	public void test3(){
		Date d = new Date();
		Calendar c = Calendar.getInstance();
		c.setTime(d);
		int m = c.get(Calendar.MINUTE);
		m = m-m%5;
		c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DATE), c.get(Calendar.HOUR_OF_DAY), m,0);
		System.out.println(c.getTime());
	}
	@Test
	public void test2(){
		ApplicationContext factory = new ClassPathXmlApplicationContext("classpath:spring/application.xml");
		BaseDao o = (BaseDao)factory.getBean("baseDao");
		Map map = new HashMap();
		map.put(1, 1);
		map.put(2, '1');
		if(map.get(1).equals(map.get(2))){
			map.clear();
		}
	}
	@Test
	public void test1() {
		try {

			Runtime rt = Runtime.getRuntime();
			Process proc = rt.exec("CMD.exe /C dir %aa%", new String[]{"aa=*.bat"},new File("c:/"));
			Process p = rt.exec("dir");
			// any error message?
			StreamGobbler errorGobbler = new StreamGobbler(p
					.getErrorStream(), "ERROR");

			// any output?
			StreamGobbler outputGobbler = new StreamGobbler(proc
					.getInputStream(), "OUTPUT");

			// kick them off
			errorGobbler.start();
			outputGobbler.start();

			// any error???
			int exitVal = proc.waitFor();
			System.out.println("ExitValue: " + exitVal);
		} catch (Throwable t) {
			t.printStackTrace();
		}
	}

	class StreamGobbler extends Thread {
		InputStream is;
		String type;

		StreamGobbler(InputStream is, String type) {
			this.is = is;
			this.type = type;
		}

		public void run() {
			try {
				InputStreamReader isr = new InputStreamReader(is);
				BufferedReader br = new BufferedReader(isr);
				String line = null;
				while ((line = br.readLine()) != null)
					System.out.println(type + ">" + line);
			} catch (IOException ioe) {
				ioe.printStackTrace();
			}
		}
	}

	@Test
	public void test() throws Exception {
		try {
			Runtime rt = Runtime.getRuntime();
			Process proc = rt.exec("cmd.exe /C dir");
			InputStreamReader isr = new InputStreamReader(proc.getInputStream());
			BufferedReader br = new BufferedReader(isr);
			String line = null;
			while ((line = br.readLine()) != null){
				System.out.println(line);
			}
			int exitVal = proc.waitFor();
			System.out.println("Process exitValue: " + exitVal);
		} catch (Throwable t) {
			t.printStackTrace();
		}

		
		File wd = new File("/bin"); 
		Process proc = null; 
		try { 
		   proc = Runtime.getRuntime().exec("/bin/bash", null, wd); 
		} 
		catch (IOException e) { 
		   e.printStackTrace(); 
		} 
		if (proc != null) { 
		   BufferedReader in = new BufferedReader(new InputStreamReader(proc.getInputStream())); 
		   PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(proc.getOutputStream())), true); 
		   out.println("cd .."); 
		   out.println("pwd"); 
		   out.println("exit"); 
		   try { 
		      String line; 
		      while ((line = in.readLine()) != null) { 
		         System.out.println(line); 
		      } 
		      proc.waitFor(); 
		      in.close(); 
		      out.close(); 
		      proc.destroy(); 
		   } 
		   catch (Exception e) { 
		      e.printStackTrace(); 
		   } 
		}
		
	}
}

